<?php
namespace app\admin\controller\office;
use app\common\controller\Admin;
use app\common\annotation\NodeAnnotation;
use app\common\traits\ControllerCurd;
use think\Request;

/**
 * @NodeAnnotation(
 *     title="工具箱",
 *     parent="admin/office.index/index",
 *     is_menu=0,
 *     is_view=0,
 *     auth=1,
 * )
 */
class Docarticle extends Admin
{

    use ControllerCurd;
    public $model = '';
    public $chapterId;


    public function initialize()
    {
        parent::initialize();
        $this->chapterId = $this->request->param("chapter_id",0,"intval");

        $this->assign([
            "chapterId" => $this->chapterId
        ]);

        $this->model = new \app\admin\model\office\DocumentArticleModel();
    }
    /**
     * @NodeAnnotation(
     *     title="文章管理",
     *     parent="admin/office.docarticle",
     *     is_menu=0,
     *     is_view=1,
     *     auth=1,
     *     MenuRoot="admin/office.index"
     * )
     */
    public function  index(Request $request)
    {
        $keywords  = $request->param("keywords");

        $where = [];

        if($keywords) array_push($where,['post_title',"like","%$keywords%"]);

        $postList = $this->model->where($where)
            ->order("update_time","desc")
            ->where("chapter_id",$this->chapterId)
            ->paginate();

        $this->assign([
            "postList"=> $postList,
            "search"  => [
                "keywords" => $keywords
            ]
        ]);

        return view();
    }

    /**
     * @NodeAnnotation(
     *     title="添加文章",
     *     parent="admin/office.docarticle",
     *     MenuRoot="admin/office.index",
     *     is_menu = 0
     * )
     */
    public function addpost(Request $request)
    {
        $this->add(url("admin/office.docarticle/index",['chapter_id'=>$this->chapterId]));
        return view();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑文章",
     *     MenuRoot="admin/office.index",
     *     parent="admin/office.docarticle",
     *     is_menu = 0
     * )
     */
    public function editpost(Request $request,$id=0)
    {
        $post = $this->model->with('user')->find($id);

        $input = $request->post();
        if(empty($input['post_status'])) $input['post_status'] = 0;
        if(empty($input['recommended'])) $input['recommended'] = 0;
        if(empty($input['is_top'])) $input['is_top'] = 0;

        $this->edit(url("admin/office.docarticle/index",['chapter_id'=>$this->chapterId]),function(){},$input);

        $this->assign([
            "post"         => $post,
        ]);
        return view();
    }

    /**
     * @NodeAnnotation(
     *     title="删除文章",
     *     parent="admin/office.docarticle",
     *     MenuRoot="admin/office.index",
     *     is_menu = 0
     * )
     */
    public function delpost()
    {
        $this->del();
    }
}